package org.metasketch.simulator.petri;

import java.util.ArrayList;
import java.util.List;

public class PetriNet extends PetriNetElement {

	Place start;

	public Place getStart() {
		return start;
	}

	public void setStart(Place start) {
		this.start = start;
	}

	public PetriNetElement getByIdentifier(String identifier) {

		List<PetriNetElement> searched = new ArrayList<PetriNetElement>();
		return recursiveGetElementByIdentifier(start, identifier, searched);

	}

	protected PetriNetElement recursiveGetElementByIdentifier(
			PetriNetNode node, String identifier, List<PetriNetElement> searched) {

		if (searched.contains(node))
			return null;

		if (node.getIdentifier().equals(identifier))
			return node;

		searched.add(node);

		List<Link> outputs = node.getOutputs();
		for (Link link : outputs) {

			// if ()
			// continue;

			if (link.getIdentifier() != null
					&& link.getIdentifier().equals(identifier))
				return link;

			PetriNetElement result = recursiveGetElementByIdentifier(
					link.getTarget(), identifier, searched);
			if (result != null)
				return result;

		}

		return null;
	}

}
